package com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.persenter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.widget.EditText;
import android.widget.ImageView;

import com.hzdesk.media.matvector.koreabatch.client.my_paperless.R;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.bean.LoginInfoBean;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.config.RBConfig;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.contract.ILoginContract;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.modle.LoginModle;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.JsonUitl;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.MyToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/8/9.
 */

public class LoginPersenter implements ILoginContract.ILoginPersemter{
    private String TAG=LoginPersenter.class.getSimpleName();
    private ILoginContract.ILoginView iLoginView;
    private Context mContext;
    //密码默认不显示
    private boolean passwordIsShow=false;
    private ILoginContract.ILoginModle mILoginModle;

    private EditText editText_username,editText_passwprd;
    private Handler handler = new Handler();

    public LoginPersenter(ILoginContract.ILoginView iLoginView) {
        this.iLoginView = iLoginView;
        mILoginModle = new LoginModle();
    }

    @Override
    public void initView(EditText username, EditText password,Context context) {
        editText_username=username;
        editText_passwprd=password;
        mContext=context;

    }

    @Override
    public void onClickedLogin() {
        Log.d(TAG,editText_passwprd.getText().toString());
       String account =  iLoginView.getAccount();
       if(!TextUtils.isEmpty(account) ){
           String password = iLoginView.getPassword();
           if(!TextUtils.isEmpty(password) ){
               submitloginInfo(account,password);
            /*  if(password.length()>=8&&password.length()<=16){

              }else{
                  iLoginView.showMessageInfo("请输入8~16位密码");
              }*/
           }else{
               iLoginView.showMessageInfo("请输入密码");
           }
       }else{
           iLoginView.showMessageInfo("请输入账号");
       }
    }

    private void submitloginInfo(String account, String password) {
        OkHttpUtils
                .post()
                .url(RBConfig.HOST_IP+RBConfig.USER_LOGINING)
                .addParams("userName", account)
                .addParams("password",password )
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetWorkResponse = JsonUitl.NetWorkResponse(response);
                        if(NetWorkResponse != null){
                            JSONObject object = JsonUitl.NetworkResponseDateDeal(NetWorkResponse);
                            if(object != null){
                                int codo = JsonUitl.getParsinCode(object);
                                if(codo == 100){
                                    LoginInfoBean loginInfoBean = (LoginInfoBean) JsonUitl.formDate(object, LoginInfoBean.class);
                                    if(loginInfoBean != null){
                                        // todo 存储token 就是 message
                                        String Message = object.getString("message");
                                        mILoginModle.saveToken(mContext,Message);
                                        //todo 存储数据
                                        mILoginModle.sveLoginInfo(mContext,loginInfoBean);
                                        mILoginModle.SaveIsChangePassword(mContext);
                                        // todo 记录是否登录过
                                        mILoginModle.saveIsLogin(mContext);
                                        handler.post(new Runnable() {
                                            @Override
                                            public void run() {
                                                iLoginView.showMessageInfo("登录成功");
                                                // 请求后台获取数据 成功跳转到后台
                                                iLoginView.loginSuccess();
                                            }
                                        });
                                    }
                                }else{
                                    final String Message = object.getString("data");
                                    // todo 注意再主线中执行才能弹框
                                    // todo 切换到主线程
                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            iLoginView.showMessageInfo(Message);
                                        }
                                    });

                                }
                            }
                        }

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                iLoginView.showMessageInfo("登录失败，请重新登录");
                            }
                        });

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });

    }

    @Override
    public void onClickedForget() {

    }

    @Override
    public void onClickedPasswordShowOrHide(ImageView imageView) {
        String password = iLoginView.getPassword();
        if(!TextUtils.isEmpty(password)){
            if (passwordIsShow){
                passwordIsShow=false;
                editText_passwprd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                editText_passwprd.setSelection(password.length());
                imageView.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(),R.mipmap.login_password_hide));
            }else {
                passwordIsShow=true;
                editText_passwprd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                editText_passwprd.setSelection(password.length());
                imageView.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(),R.mipmap.login_password_show));
            }
           /* if(password.length()>=8&&password.length()<=16){

            }else{
                iLoginView.showMessageInfo("输入的密码长度正确");
            }*/
        }else{
            iLoginView.showMessageInfo("请先输入密码");
        }


    }

}
